package nl.changer.audiowifedemo_ohos.slice;

import nl.changer.audiowife.AudioWife;
import nl.changer.audiowifedemo_ohos.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.media.player.Player;
import ohos.utils.net.Uri;

import java.io.File;

import static nl.changer.audiowifedemo_ohos.MainAbility.TEST_AUDIO_FILE_PATH;

public class DefaultPlayerAbilitySlice extends BaseAbilitySlice {
    private static final String TAG = DefaultPlayerAbilitySlice.class.getSimpleName();

    private static final int INTENT_PICK_AUDIO = 1;

    private Context mContext;
    private ComponentContainer mPlayerContainer;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_awd_default_player);

        mContext = DefaultPlayerAbilitySlice.this;

        Component pickAudio = findComponentById(ResourceTable.Id_pickAudio);

        pickAudio.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mUri == null) {
                    pickAudio();
                }
            }
        });

        // this will act as container for default
        // audio player UI.
        mPlayerContainer = (ComponentContainer) findComponentById(ResourceTable.Id_player_layout);
    }

    private void pickAudio() {
        //todo 跳转到文件管理器，选择音频文件,当前ohos不支持该功能
        //模拟选择了音频文件
        File file = new File(TEST_AUDIO_FILE_PATH);
        Uri uri = Uri.getUriFromFile(file);
        playAudio(uri);
        new ToastDialog(this).setText("Pick Audio Success").show();
    }

    private void playAudio(Uri uri) {
        mUri = uri;
        // mPlayerContainer = View to integrate default player UI into.
        AudioWife.getInstance().init(mContext, uri).useDefaultUi(mPlayerContainer, LayoutScatter.getInstance(mContext));

        AudioWife.getInstance().addOnCompletionListener(new Player.IPlayerCallback() {
            @Override
            public void onPrepared() {

            }

            @Override
            public void onMessage(int i, int i1) {

            }

            @Override
            public void onError(int i, int i1) {

            }

            @Override
            public void onResolutionChanged(int i, int i1) {

            }

            @Override
            public void onPlayBackComplete() {
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        new ToastDialog(mContext).setText("Completed").show();
                    }
                });
                // do you stuff.
            }

            @Override
            public void onRewindToComplete() {

            }

            @Override
            public void onBufferingChange(int i) {

            }

            @Override
            public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

            }

            @Override
            public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

            }
        });

        AudioWife.getInstance().addOnPlayClickListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(mContext).setText("Play").show();
                // get-set-go. Lets dance.
            }
        });

        AudioWife.getInstance().addOnPauseClickListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(mContext).setText("Pause").show();
                // Your on audio pause stuff.
            }
        });
    }
}
